home *** CD-ROM | disk | FTP | other *** search
/ Delphi 2.0 - Programmer's Utilities Power Pack / Delphi 2.0 Programmer's Utilities Power Pack.iso / s_to_z / winrst / winrstor.pas
Encoding:
Pascal/Delphi Source File  |  1996-09-15  |  7.5 KB  |  200 lines

  1. unit WinRstor; {WindowRestorer - Window size and state restorer}
  2. {DESCRIPTION:  Ever notice how professional programs seem to remember in what
  3. condition and location you left them and their child windows?  Ever notice how
  4. most RAD apps don't?  Take that ragged edge off your program with this unit.  It
  5. Allows apps to save the location, size, and state of windows so that when the
  6. user reopens them, they will look as the user left them.
  7.  
  8. DISLCAIMERS:  By using this code you accept full responsibility for understanding
  9. how it works and understand that no warrantees, explicit or implicit, are made
  10. as to it's suitability or fitness for any task.  In other words, use it at your own
  11. risk, and understand how it works before you use it.  Go ahead and distribute it,
  12. improve it, anything.  Just don't patent it.  This is independent work, but basic
  13. stuff to crisp windows programming.
  14.  
  15. USE: Put WINRSTOR in the uses of clause of your main form and any forms that will
  16. be saving or restoring their own state, size, or location.  (If you will be doing
  17. all the saving and restoring using WinSaveChildren and WinRestoreChildren from the
  18. main form, you only need reference it in the main form's uses clause.)
  19.  
  20. In MainForm.Create, initialize the global WinRestorer object as follows (it's already
  21. declared in this file, but needs to be allocated):
  22.     GlobalWinRestorer := TWinRestorer.create( Application, TRUE, WHATSAVE_ALL);
  23. Which is the same as:
  24.     GlobalWinRestorer := TWinRestorer.create( Application, TRUE, [location, size, state]);
  25. Then, in MainForm.Destroy, deallocate the global WinRestorer object as follows:
  26.     GlobalWinRestorer.free;
  27.  
  28.  
  29. A good place to save a form's status is in the queryclose event or else attached
  30. to a button or menu item.  I usually create an item in the File Menu captioned
  31. 'Save &Workspace' which does:
  32.     GlobalWinRestorer.SaveChildren( Self, [default]);
  33. And under main form's Close event I put:
  34.     GlobalWinRestorer.SaveWin(Self, [WHATSAVE_ALL]);
  35.  
  36. I have tended to restore the children's status in their own show events like this:
  37.     GlobalWinRestorer.RestoreWin(Self, [default]);
  38. though I am moving toward putting in the main form's show event:
  39.     GlobalWinRestorer.RestoreWin(Self, [default]);
  40.     GlobalWinRestorer.RestoreChildren(Self, [default]);
  41.  
  42. HINTS:  If you set TForm.Position to poScreenCenter or anything fancy, this unit
  43. won't do what you expect.  poDesigned seems to work pretty good.  I could have raised
  44. an exception if you try to set top and left of a poScreenCentere'd form, but then you
  45. have to be careful using WinRestoreChildren.  I opted not to check the position property
  46. and leave that up to individual developers.
  47.  
  48. This is freeware, and I'm not sure how much time I have to support questions, but
  49. my CIS address is Brad Olson 75512,2366.
  50.  
  51. Last Revised March 23, 1995 for Delphi 1.0
  52.  
  53. }
  54.  
  55. INTERFACE
  56.  
  57. USES SysUtils, Forms;
  58.  
  59. TYPE {=============================================================}
  60.  
  61. {------------------------------------------------------------------
  62. Windows restorer object class and related types.
  63. -------------------------------------------------------------------}
  64. EWinRestorer = class( Exception);
  65. TWhatSave = (default, size, location, state);
  66. STWhatSave = set of TWhatSave;
  67. TWinRestorer = class(TObject)
  68.  protected
  69.     mIniFile: string;
  70.     mIniSect: string[80];
  71.     mIsInitialized: boolean;
  72.     mDefaultWhat: STWhatSave;
  73.  public
  74.     constructor Create( TheApp: TApplication;
  75.         LocalDir: boolean; DefaultWhatSave: STWhatSave);
  76.         {If localDir is true, ini dir is the app dir.  Else, ini dir is the windows dir.}
  77.     procedure SaveWin(TheForm: TForm; What: STWhatSave);
  78.     procedure SaveChildren(TheMDIForm: TForm; What: STWhatSave);
  79.     procedure RestoreWin( TheForm: TForm; What: STWhatSave);
  80.     procedure RestoreChildren(TheMDIForm: TForm; What: STWhatSave);
  81.     property IniFileName: string  read mIniFile;
  82. end;
  83.  
  84. CONST
  85.     WHATSAVE_ALL = [size, location, state];
  86.  
  87. VAR
  88. GlobalWinRestorer: TWinRestorer;
  89.  
  90. IMPLEMENTATION
  91.  
  92. Uses IniFiles;
  93.  
  94. constructor TWinRestorer.create;
  95. var fname, path: string[100];
  96. begin
  97.     inherited create;
  98. {Calculate ini file name}
  99.     if default in DefaultWhatSave then
  100.         raise EWinRestorer.create(
  101.          'Attempt to initialize default window position paramaters with set ' +
  102.          ' containing [default] item.  ' +
  103.          'Default params may contain only members of [size, location, state].  ')
  104.     else mDefaultWhat := DefaultWhatSave;
  105.     fname := ChangeFileExt( ExtractFileName( TheApp.exeName), '.INI');
  106.     if LocalDir then begin {parse out path and add to file name}
  107.         path := ExtractFilePath(TheApp.exeName);
  108.         if path[length(path)] <> '\' then
  109.             path := path + '\';
  110.         fname := path + fname;
  111.     end;
  112. {fill object fields}
  113.     mIniFile := fname;
  114.     mIniSect := 'WindowsRestorer';
  115. {It'd be nice to write some notes to a section called [WinRestorer Notes]}
  116. end;
  117.  
  118. procedure TWinRestorer.RestoreWin;
  119. var FormNm, SectionNm: string[80];     ini: TIniFile;
  120.     n,l,t,w,h: integer; {Left, Top Width, Height}
  121. begin
  122.     ini := TIniFile.create( mIniFile);
  123.     TRY
  124.         SectionNm := mIniSect;
  125.         FormNm := TheForm.classname;
  126.         if default in What then What := mDefaultWhat;
  127. {Update Window State if Necessary}
  128.         if state in What then
  129.             n := ini.ReadInteger( SectionNm, FormNm + '_WindowState', 0);
  130.             case  n of
  131.               1:     TheForm.WindowState := wsMinimized;
  132.               2:    TheForm.WindowState := wsNormal;
  133.               3:     TheForm.WindowState := wsMaximized;
  134.             end;
  135. {Update Size and Location if necessary.}
  136.         with TheForm do begin l:=left; t:=top; h:=height; w:=width; end; {Save current vals.}
  137.         if size in What then begin
  138.           w := ini.ReadInteger( SectionNm, FormNm + '_Width', w);
  139.           h := ini.ReadInteger( SectionNm, FormNm + '_Height', h);
  140.         end;
  141.         if location in What then begin
  142.           t := ini.ReadInteger( SectionNm, FormNm + '_Top', t);
  143.           l := ini.ReadInteger( SectionNm, FormNm + '_Left', l);
  144.         end;
  145.         TheForm.SetBounds(l,t,w,h);
  146.     FINALLY
  147.         ini.free;
  148.     END;
  149. end;
  150.  
  151. procedure TWinRestorer.RestoreChildren;
  152. var i: integer;
  153. begin
  154.     if TheMDIForm.formstyle <> fsMDIForm then
  155.         raise EWinRestorer.create('Attempting to save window sizes of children for a non MDI parent window.')
  156.     else
  157.         for i := 0 to TheMDIForm.MDIChildCount - 1 do
  158.             RestoreWin( TheMDIForm.MDIChildren[i], what);
  159. end;
  160.  
  161. procedure TWinRestorer.SaveWin;
  162. var FormNm, SectionNm: string[80];     w : STWhatsave; ini: TIniFile;
  163. begin
  164.     ini := TIniFile.create( mIniFile);
  165.     TRY
  166.         SectionNm := mIniSect;
  167.         FormNm := TheForm.ClassName;
  168.         if default in What then w := mDefaultWhat else w := mDefaultWhat;
  169.         if size in w then begin
  170.           ini.WriteInteger( SectionNm, FormNm + '_Width', TheForm.Width);
  171.           ini.WriteInteger( SectionNm, FormNm + '_Height', TheForm.Height);
  172.         end;
  173.         if location in w then begin
  174.           ini.WriteInteger( SectionNm, FormNm + '_Top', TheForm.Top);
  175.           ini.WriteInteger( SectionNm, FormNm + '_Left', TheForm.Left);
  176.         end;
  177.         if state in w then
  178.           case TheForm.WindowState of
  179.               wsMinimized:     ini.WriteInteger( SectionNm, FormNm + '_WindowState', 1);
  180.               wsNormal:         ini.WriteInteger( SectionNm, FormNm + '_WindowState', 2);
  181.               wsMaximized:     ini.WriteInteger( SectionNm, FormNm + '_WindowState', 3);
  182.           end;
  183.     FINALLY
  184.         ini.free;
  185.     END;
  186. end;
  187.  
  188. procedure TWinRestorer.SaveChildren;
  189. var i: integer;
  190. begin
  191.     if TheMDIForm.formstyle <> fsMDIForm then
  192.       raise EWinRestorer.create('Attempting to restore window sizes of children for a non MDI parent window.')
  193.     else
  194.         for i := 0 to TheMDIForm.MDIChildCount - 1 do
  195.             SaveWin( TheMDIForm.MDIChildren[i], what);
  196. end;
  197.  
  198. INITIALIZATION
  199. END.
  200.